package cn.edu.xmut.chen.zhlg.zhlg.vacancy.mapper;

import cn.edu.xmut.chen.zhlg.zhlg.vacancy.entity.Vacancy;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.result.VacancyPageResult;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface VacancyMapper extends BaseMapper<Vacancy> {


    Page<VacancyPageResult> pageSearch(Page<VacancyPageResult> page,
                                       @Param("vacancyName") String vacancyName,
                                       @Param("posterId") String posterId,
                                       @Param("companyName") String companyName,
                                       @Param("address") String address,
                                       @Param("industry") String industry,
                                       @Param("level") String level,
                                       @Param("minSalary") String minSalary,
                                       @Param("maxSalary") String maxSalary);

    @Select("<script>" +
            "SELECT v.id, v.vacancy_name, c.address, v.detailed, v.salary, v.create_time, v.level, " +
            "u.id AS posterId, " +
            "c.company_name AS company, c.`condition` AS companyCondition, c.number AS companyNumber, " +
            "c.industry AS companyIndustry, c.id AS companyId, c.pic AS companyPic, " +
            "u.nick_name AS poster_name, u.phone AS poster_phone, u.pic AS poster_pic " +
            "FROM tb_vacancy v " +
            "LEFT JOIN tb_user u ON v.poster_id = u.id " +
            "LEFT JOIN tb_company c ON v.company_id = c.id " +
            "WHERE v.delete_flag = 'NOT_DELETE' " +
            "<if test='id != null'>" +
            "AND v.id = #{id}" +
            "</if>" +
            "ORDER BY v.create_time DESC" +
            "</script>")
    VacancyPageResult getDetail(@Param("id") String id);

    @Select("<script>" +
            "SELECT v.id, v.vacancy_name, c.address, v.detailed, v.salary, v.create_time, v.level, " +
            "v.poster_id AS posterId, " +
            "c.company_name AS company, c.`condition` AS companyCondition, c.number AS companyNumber, " +
            "c.industry AS companyIndustry, c.id AS companyId, c.pic AS companyPic, " +
            "u.nick_name AS poster_name, u.phone AS poster_phone, u.pic AS poster_pic " +
            "FROM tb_vacancy v " +
            "LEFT JOIN tb_user u ON v.poster_id = u.id " +
            "LEFT JOIN tb_company c ON v.company_id = c.id " +  // 新增公司表关联
            "WHERE v.delete_flag = 'NOT_DELETE' " +
            "   AND v.id != #{id} " +
            "ORDER BY v.create_time DESC " +
            "</script>")
    Page<VacancyPageResult> pageSearchOther(Page<VacancyPageResult> page, String id);
}


//package cn.edu.xmut.chen.zhlg.zhlg.vacancy.mapper;
//
//import cn.edu.xmut.chen.zhlg.zhlg.vacancy.entity.Vacancy;
//import cn.edu.xmut.chen.zhlg.zhlg.vacancy.result.VacancyPageResult;
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import org.apache.ibatis.annotations.Mapper;
//import org.apache.ibatis.annotations.Param;
//import org.apache.ibatis.annotations.Select;
//
//@Mapper
//public interface VacancyMapper extends BaseMapper<Vacancy> {
//
//    @Select("<script>" +
//            "SELECT v.id, v.vacancy_name, c.address, v.detailed, v.salary, v.create_time, v.level, " +
//            "c.company_name AS company, c.`condition` AS companyCondition, c.number AS companyNumber, " +
//            "c.industry AS companyIndustry, c.id AS companyId, c.pic AS companyPic, " +
//            "u.nick_name AS poster_name, u.phone AS poster_phone, u.pic AS poster_pic, " +
//            "(SELECT COUNT(*) FROM tb_apply a WHERE a.job_id = v.id) as apply_count " +
//            "FROM tb_vacancy v " +
//            "LEFT JOIN tb_user u ON v.poster_id = u.id " +
//            "LEFT JOIN tb_company c ON v.company_id = c.id " +
//            "WHERE v.delete_flag = 'NOT_DELETE' " +
//            "<if test='vacancyName != null and vacancyName != \"\"'>" +
//            "   AND (v.vacancy_name LIKE CONCAT('%', #{vacancyName}, '%') " +
//            "   OR c.company_name LIKE CONCAT('%', #{vacancyName}, '%')) " +
//            "</if>" +
//            "<if test='posterId != null'>" +
//            "   AND v.poster_id = #{posterId} " +
//            "</if>" +
//            "<if test='companyName != null and companyName != \"\"'>" +
//            "   AND c.company_name LIKE CONCAT('%', #{companyName}, '%') " +
//            "</if>" +
//            "<if test='address != null and address != \"\"'>" +
//            "   AND c.address LIKE CONCAT('%', #{address}, '%') " +
//            "</if>" +
//            "<if test='industry != null and industry != \"\"'>" +
//            "   AND c.industry LIKE CONCAT('%', #{industry}, '%') " +
//            "</if>" +
//            "<if test='level != null and level != \"\"'>" +
//            "   AND v.level = #{level} " +
//            "</if>" +
//            "<if test='minSalary != null and minSalary != \"\"'>" +
//            "   AND CAST(REPLACE(REPLACE(v.salary, 'K', ''), '-', '') AS DECIMAL) >= #{minSalary} " +
//            "</if>" +
//            "<if test='maxSalary != null and maxSalary != \"\"'>" +
//            "   AND CAST(REPLACE(REPLACE(v.salary, 'K', ''), '-', '') AS DECIMAL) <= #{maxSalary} " +
//            "</if>" +
//            "ORDER BY v.create_time DESC " +
//            "</script>")
//    Page<VacancyPageResult> pageSearch(Page<VacancyPageResult> page,
//                                       @Param("vacancyName") String vacancyName,
//                                       @Param("posterId") String posterId,
//                                       @Param("companyName") String companyName,
//                                       @Param("address") String address,
//                                       @Param("industry") String industry,
//                                       @Param("level") String level,
//                                       @Param("minSalary") String minSalary,
//                                       @Param("maxSalary") String maxSalary);
//
//    @Select("<script>" +
//            "SELECT v.id, v.vacancy_name, c.address, v.detailed, v.salary, v.create_time, v.level, " +
//            "u.id AS posterId, " +
//            "c.company_name AS company, c.`condition` AS companyCondition, c.number AS companyNumber, " +
//            "c.industry AS companyIndustry, c.id AS companyId, c.pic AS companyPic, " +
//            "u.nick_name AS poster_name, u.phone AS poster_phone, u.pic AS poster_pic " +
//            "FROM tb_vacancy v " +
//            "LEFT JOIN tb_user u ON v.poster_id = u.id " +
//            "LEFT JOIN tb_company c ON v.company_id = c.id " +
//            "WHERE v.delete_flag = 'NOT_DELETE' " +
//            "<if test='id != null'>" +
//            "AND v.id = #{id}" +
//            "</if>" +
//            "ORDER BY v.create_time DESC" +
//            "</script>")
//    VacancyPageResult getDetail(@Param("id") String id);
//
//    @Select("<script>" +
//            "SELECT v.id, v.vacancy_name, c.address, v.detailed, v.salary, v.create_time, v.level, " +
//            "v.poster_id AS posterId, " +
//            "c.company_name AS company, c.`condition` AS companyCondition, c.number AS companyNumber, " +
//            "c.industry AS companyIndustry, c.id AS companyId, c.pic AS companyPic, " +
//            "u.nick_name AS poster_name, u.phone AS poster_phone, u.pic AS poster_pic " +
//            "FROM tb_vacancy v " +
//            "LEFT JOIN tb_user u ON v.poster_id = u.id " +
//            "LEFT JOIN tb_company c ON v.company_id = c.id " +  // 新增公司表关联
//            "WHERE v.delete_flag = 'NOT_DELETE' " +
//            "   AND v.id != #{id} " +
//            "ORDER BY v.create_time DESC " +
//            "</script>")
//    Page<VacancyPageResult> pageSearchOther(Page<VacancyPageResult> page, String id);
//}
